package com.taiji.dicom4cloud.smart.camera;

import com.alibaba.fastjson.JSONObject;
import com.taiji.dicom4cloud.smart.config.CameraApi;
import com.taiji.dicom4cloud.smart.service.SysConfigService;
import com.taiji.dicom4cloud.smart.utils.FFmpegUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;


/**
 * @ClassName AsyncTaskService
 * @Description
 * @Author HX
 * @Date 2021/9/18 13:55
 **/
@Service
@Slf4j
public class AsyncTaskService {

    @Autowired
    private Executor asyncTaskExecutor;    //注入线程池的bean

    @Autowired
    private SysConfigService sysConfigService;

    @Async
    public CompletableFuture capture2(String deviceId){
        return CompletableFuture.supplyAsync(()->{   //supplyAsync()  支持返回结果    runAsync()  不支持返回结果
            //具体要执行的逻辑
            log.info(">>>>>>>>>>>>>>开始抓图>>>>>>>>>>>>>>"+Thread.currentThread().getName());
            String captureUrl = "";
            try {

                Map<String, Object> map = sysConfigService.getConfigByKeys("client_id,client_secret");
                String token = CameraApi.getToken(String.valueOf(map.get("client_id")),String.valueOf(map.get("client_secret")));
                Thread.sleep(500);
                JSONObject jsonObject = CameraApi.getChannelSnapPic(deviceId,"0",token);
                if(jsonObject.getBoolean("success")){
                    captureUrl = jsonObject.getString("data");
                    log.info(token);
                }
            } catch (Exception e) {
                log.info(e.getMessage());
                e.printStackTrace();
            }
            return captureUrl;
        },asyncTaskExecutor);
    }

    @Async
    public CompletableFuture videoConvert(String inPath,String outPath){
        return CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(30000);//ftp上传需要延迟等待
                log.info(">>>>>>>>>>>>>>开始视频转码>>>>>>>>>>>>>>"+Thread.currentThread().getName());
                FFmpegUtils.videoConvert(inPath,outPath);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("转码失败：{}",e.getMessage());
            }
            return "success";
        },asyncTaskExecutor);
    }
    @Async
    public CompletableFuture videoScreenShot(String inPath,String outPath){
        return CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(30000);//ftp上传需要延迟等待
                log.info(">>>>>>>>>>>>>>抓图处理>>>>>>>>>>>>>>"+Thread.currentThread().getName());
                FFmpegUtils.videoScreenShot(inPath,outPath);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("抓图处理失败：{}",e.getMessage());
            }
            return "success";
        },asyncTaskExecutor);
    }

    @Async
    public CompletableFuture createHls(JSONObject jsonObject){
        String deviceId = jsonObject.getString("deviceId");
        String channelId =  jsonObject.getString("channelId");
        String beginTime =  jsonObject.getString("beginTime");
        String endTime =  jsonObject.getString("endTime");
        return CompletableFuture.supplyAsync(()->{   //supplyAsync()  支持返回结果    runAsync()  不支持返回结果
            //具体要执行的逻辑
            log.info(">>>>>>>>>>>>>>生成HLS视频地址>>>>>>>>>>>>>>"+Thread.currentThread().getName());
            String hlsUrl = "";
            try {
                Thread.sleep(1000);
                Map<String, Object> map = sysConfigService.getConfigByKeys("client_id,client_secret");
                String token = CameraApi.getToken(String.valueOf(map.get("client_id")),String.valueOf(map.get("client_secret")));
                JSONObject jsonObject2 = CameraApi.createDeviceRecordHls(deviceId,channelId,beginTime,endTime,token);
                //保存hls地址
                if(jsonObject2.getBoolean("success")){
                    hlsUrl = jsonObject2.getJSONObject("data").getString("url");
                }
            } catch (Exception e) {
                log.info(e.getMessage());
                e.printStackTrace();
            }
            return hlsUrl;
        },asyncTaskExecutor);
    }
}